package com.hm.service.request.test;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.service.http.HttpUtils;
import com.hm.service.utils.CBC_Encryption;
import com.hm.service.utils.EncryptionUtils;
import com.hm.service.utils.JsonUtils;
import com.hm.service.utils.Md5Utils;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.HexFormat;


/**
 * 获取session接口
 */
public class Request07 {

    private static final String url = "http://172.26.2.18:9001/risk/main/000_000_000";
    private static final String secretKey = HexFormat.of().formatHex("kokL8G4RmNfV69cdcJ9dfnrlFGsLy6mH".getBytes());//转为十六进制字符串


    public static void main(String[] args) {
        //请求参数
        String jsonObject = getJsonObject();
        //构建请求体
        RequestBody body = RequestBody.create(jsonObject, MediaType.get("application/json; charset=UTF-8"));
        Request request = new Request.Builder()//
                .url(url)//请求连接
                .post(body)//请求体
                .build();//构建
        //同步请求
        OkHttpClient okHttpClient = HttpUtils.getOkHttpClient();
        try (Response response = okHttpClient.newCall(request).execute()) {
            ResponseBody resBody = response.body();
            if (resBody != null) {
                String jsonString = resBody.string();
                boolean json = JsonUtils.isJson(jsonString);
                System.out.println("1ResponseBody-->:" + jsonString);
                System.out.println(json ? jsonString : CBC_Encryption.decrypt(jsonString, secretKey));
            } else {
                System.out.println("ResponseBody为空");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static String getJsonObject() {
        String timestamp = String.valueOf(System.currentTimeMillis());
        String appVersion = "111";
        String channel = "Android";//通道标识
        String osVersion = "111";
        String apiLevel = "111";
        String screenWidth = "111";
        String screenHeight = "111";
        String manufacturer = "111";
        String deviceUniqueId = "111222";

        String signature = timestamp.toLowerCase() //
                + screenHeight.toLowerCase() //
                + appVersion.toLowerCase()//
                + osVersion.toLowerCase() //
                + deviceUniqueId.toLowerCase()//
                + screenWidth.toLowerCase()//
                + channel.toLowerCase()//
                + manufacturer.toLowerCase()//
                + apiLevel.toLowerCase()//
                + timestamp.toLowerCase(); //

        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("timestamp", timestamp);
        paramsMap.put("appVersion", appVersion);
        paramsMap.put("channel", channel);
        paramsMap.put("osVersion", osVersion);
        paramsMap.put("apiLevel", apiLevel);
        paramsMap.put("screenWidth", screenWidth);
        paramsMap.put("screenHeight", screenHeight);
        paramsMap.put("manufacturer", manufacturer);
        paramsMap.put("deviceUniqueId", deviceUniqueId);
        paramsMap.put("signature", Md5Utils.generateMD5(signature));
        return JSON.toJSONString(paramsMap);
    }
}